package talk.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import talk.Bean.Register;
import talk.Net.HttpRequest;
import com.talk.debby.talk.R;
import talk.Utils.FormatUtil;

import base.BaseActivity;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by debby on 2018/4/16.
 * 描述:注册
 */

public class RegisterActivity extends BaseActivity {
    private EditText et_name;
    private EditText et_password;
    private EditText et_confirm_password;
    private EditText et_email;

    private TextView tv_uid;
    private Button btn_login;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_register);
        initView();
    }

    private void initView() {
        et_name = findViewById(R.id.et_name);
        et_password = findViewById(R.id.et_password);
        et_confirm_password = findViewById(R.id.et_confirm_password);
        et_email = findViewById(R.id.et_email);

        tv_uid = findViewById(R.id.tv_uid);
        btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        findViewById(R.id.btn_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
    }

    //注册
    private void register() {
        String name = et_name.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        String confirm_password = et_confirm_password.getText().toString().trim();
        String email = et_email.getText().toString().trim();

        if (TextUtils.isEmpty(name)) {
            showToast("请输入昵称");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        if (TextUtils.isEmpty(confirm_password)) {
            showToast("请输入密码");
            return;
        }
        if (!confirm_password.equals(password)) {
            showToast("密码不一致");
            return;
        }
        if (TextUtils.isEmpty(email)) {
            showToast("请输入邮箱");
            return;
        }
        if (TextUtils.isEmpty(email)) {
            showToast("请输入邮箱");
            return;
        }

        if (!FormatUtil.isEmail(email)) {
            showToast("邮箱格式不正确");
            return;
        }
        showProgressDialog("注册中");
        HttpRequest.register(name, password, email
                , "https://debby.oss-cn-beijing.aliyuncs.com/pikc.png",
                new Observer<Register>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Register register) {
                        if (register.getStatus() == 300) {
                            showToast(register.getMsg());
                            tv_uid.setVisibility(View.VISIBLE);
                            btn_login.setVisibility(View.VISIBLE);
                            tv_uid.setText(register.getUid());
                            setUserID(register.getUid());
                        } else {
                            showToast(register.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast("失败");
                        dismissProgressDialog();

                    }

                    @Override
                    public void onComplete() {
                        dismissProgressDialog();
                    }
                });


    }

}
